package com.huxiaohui.ssbx.agent;

import com.huxiaohui.ssbx.agent.appoperation.AppOperationAgent;
import com.huxiaohui.ssbx.agent.enums.AgentTypeEnum;
import com.huxiaohui.ssbx.agent.filesystem.FileSystemAgent;
import com.huxiaohui.ssbx.agent.openapp.OpenAppAgent;
import com.huxiaohui.ssbx.agent.planning.ReActPlanningAgent;
import com.huxiaohui.ssbx.agent.supportopertionsapp.GetSupportOperationAppAgent;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

@Configuration
public class AgentFactory {

    public static final Map<AgentTypeEnum, Function<String, Agent>> AGENT_FUNC_MAP = new HashMap<>();

    /**
     * 初始化方法，完成Agent的注册
     */
    @PostConstruct
    public void init() {
        // 注册任务规划智能体
        AGENT_FUNC_MAP.put(AgentTypeEnum.RE_ACT_PLANNING_AGENT, this::reActPlanningAgent);
        AGENT_FUNC_MAP.put(AgentTypeEnum.OPEN_APP_AGENT, this::openAppAgent);
        AGENT_FUNC_MAP.put(AgentTypeEnum.FILE_SYSTEM_AGENT, this::fileSystemAgent);
        AGENT_FUNC_MAP.put(AgentTypeEnum.APP_OPERATION_AGENT, this::appOperationAgent);
        AGENT_FUNC_MAP.put(AgentTypeEnum.Get_Support_Operation_App_Agent, this::getsupportOperationAppAgent);
    }

    /**
     * 根据agentTypeEnum获取对应的Agent
     */
    public static Function<String, Agent> getAgent(AgentTypeEnum agentTypeEnum) {
        Function<String, Agent> fun = AGENT_FUNC_MAP.get(agentTypeEnum);
        if (null == fun) {
            throw new IllegalArgumentException("找不到对应的智能体: " + agentTypeEnum);
        }
        return fun;
    }

    /**
     * 任务规划智能体，交由Spring管理
     *
     * @param sessionId 会话id
     * @return 查找到的智能体实例
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Agent reActPlanningAgent(String sessionId) {
        return new ReActPlanningAgent(sessionId);
    }

    /**
     * 打开应用或文件智能体，交由Spring管理
     *
     * @param sessionId 会话id
     * @return 查找到的智能体实例
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Agent openAppAgent(String sessionId) {
        return new OpenAppAgent(sessionId);
    }

    /**
     * 文件系统智能体，交由Spring管理
     *
     * @param sessionId 会话id
     * @return 查找到的智能体实例
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Agent fileSystemAgent(String sessionId) {
        return new FileSystemAgent(sessionId);
    }

    /**
     * 桌面操作智能体，交由Spring管理
     *
     * @param sessionId 会话id
     * @return 查找到的智能体实例
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Agent appOperationAgent(String sessionId) {
        return new AppOperationAgent(sessionId);
    }

    /**
     * 获取支持操作应用的智能体，交由Spring管理
     *
     * @param sessionId 会话id
     * @return 查找到的智能体实例
     */
    @Bean
    @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public Agent getsupportOperationAppAgent(String sessionId) {
        return new GetSupportOperationAppAgent(sessionId);
    }

}
